/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package dw2;
import java.awt.Color;
import java.awt.Image;
import java.awt.Point;
import java.util.ArrayList;
import java.util.Arrays;

/**
 *
 * @author Brian
 */
public class npcToad extends NPC {
    protected int health = 10; //Health works now!
    protected static ArrayList<Image> image = new ArrayList<Image>();
    protected int cycle_count = 0;
    protected int hurt_count = 0;
    protected Particle particles[] = new Particle[500];
    protected int particle_life = 60;
    protected double dY = 0;
    protected int toadDuration = 1000;
    protected int bombSpacing = 200;
    protected int hurtLength = 35;


    public static void initImage(Image images[])
    {
        image.addAll(Arrays.asList(images));

    }


    npcToad(int x, int y, int direction_to_face,int points){
         super(x,y-image.get(0).getHeight(null),direction_to_face,points);
          this.WALK_SPEED = 3;
          ATTACK_POWER = 10;
          FIRING_RATE = 100;
          particle_life = 120;

          //graphics.add(new DWGraphics(image.get(0)));
          graphics.add(new DWGraphics_Animation(image.get(0),25,3)); //Drive
          graphics.add(new DWGraphics_Animation(image.get(1),10,2)); //Dig
          graphics.add(new DWGraphics_Animation(image.get(2),25,3)); //Hurt

    }
    public void action(Main mainparent,Worm player)
    {
        hurt_count++;
        cycle_count++;
        this.player = player;
        
        if(state == 0){
            Drive();
            move(mainparent,player.getHeadPosition());      
        }
        
        if(state == 1){
            Dig();
            dY += 0.15;
            this.y += this.WALK_SPEED + dY;
            if(this.y > 500)  explode(mainparent, player);
  
        }
        if(state == 2){
            hurt_count++;
            Hurt();
            move(mainparent,player.getHeadPosition());

        }
       
        if(player.hitWorm(graphics.get(graphics_index).getBounds())== true && state == 1) {
            explode(mainparent, player);
            player.hurtWorm(50);
        }
        if(player.hitHead(graphics.get(graphics_index).getBounds())==true && state == 0)
        {
            health--;
            mainparent.toadHit.play();
            state = 2;
            cycle_count = 0;
            if(health == 0){
                new AePlayWave("Sounds\\bit1.wav").start();
                player.add_points(points);
                player.countKill();
                deathParticles(mainparent,player);
                mainparent.removeNPC(this);
                mainparent.toadOnScreen = false;
            }
            return;
        }
        if(cycle_count > 50){
            if(state != 1){
                state = 0;
            }

            cycle_count = 0;
        }

        

        if(state == 0 && cycle_count % bombSpacing == 0) {
                mainparent.npcs.add(new Bomb(this.x, this.y + 64, 1, 0));
                mainparent.counts++;
        }
        
        if(hurt_count > toadDuration){
            state = 1;
        }

        if(hurt_count > hurtLength) {
            state = 0;
            hurt_count = 0;
        }
    }

    public void Drive(){
        graphics_index = 0;
    }
    public void Hurt() {
        graphics_index = 2;

    }
    public void Dig(){
        graphics_index = 1;
    }

    public void deathParticles(Main mainparent,Worm player)
    {
        mainparent.bang.play();
        mainparent.toadOnScreen = false;
        int center_x = x + graphics.get(graphics_index).getWidth()/2;
        int center_y = y + graphics.get(graphics_index).getHeight();
        for(int w = 0;w<death_particle_count;w++)
        {
            Particle p = new Particle(center_x,center_y,Color.RED,2,5);
            mainparent.addParticle(p);
            mainparent.addParticle(new Particle(center_x,center_y,Color.ORANGE,2,5));
            mainparent.addParticle(new Particle(center_x,center_y,Color.YELLOW,2,5));
            mainparent.addParticle(new Particle(center_x,center_y,Color.BLACK,2,5));
            if(w == 0) particles[w] = p;
            // mainparent.addParticle(new Particle(center_x,center_y,Color.,2,5));
            //BROWN...?

        }

    }



   public void explode(Main mainparent,Worm player) {

        
        mainparent.toadOnScreen = false;
        mainparent.bigBang.play();
        int center_x = x + graphics.get(graphics_index).getWidth()/2;
        int center_y = y + graphics.get(graphics_index).getHeight();

        for(int w = 0;w<death_particle_count;w++)
        {
            Particle pR = new Particle(center_x,center_y,Color.RED,2,5);
            pR.toggleRemoveOnGround();
            pR.setLife(particle_life);
            pR.setGravity(0.1);
            mainparent.addParticle(pR);
            particles[w] = pR;

            Particle pO = new Particle(center_x,center_y,Color.ORANGE,2,5);
            pO.toggleRemoveOnGround();
            pO.setLife(particle_life);
            pO.setGravity(0.1);
            mainparent.addParticle(pO);
            particles[w+1] = pO;

           // mainparent.addParticle(new Particle(center_x,center_y,Color.,2,5));

            Particle pY = new Particle(center_x,center_y,Color.YELLOW,2,5);
            pY.toggleRemoveOnGround();
            pY.setLife(particle_life);
            pY.setGravity(0.1);
            mainparent.addParticle(pY);
            particles[w+2] = pO;

            Particle pB = new Particle(center_x,center_y,Color.BLACK,2,5);
            pB.toggleRemoveOnGround();
            pB.setLife(particle_life);
            pB.setGravity(0.1);
            mainparent.addParticle(pB);
            particles[w+3] = pB;


        }
        mainparent.removeNPC(this);
    }

    public boolean fire_gun(Main mainparent,Point head_pos){
        bulletY = y + 5;

        if(head_pos.y < mainparent.getHeight()/2 && x > player.getXpos() && direction == 1)
        {
            bulletX = x + 5;
            fireCount++;
            if(fireCount == FIRING_RATE)
            {
                new AePlayWave("Sounds\\sndGun.wav").start();
                Bullet bullet_to_fire = new Bullet(mainparent,bulletX, bulletY, Color.black, bullet_size,ATTACK_POWER);
                double bullet_direction = Worm.pointDirection(x, y, head_pos.x, head_pos.y);
                double speed = ATTACK_BULLET_SPEED;
                bullet_to_fire.setXYspeed(Worm.getXspeed(bullet_direction,speed), Worm.getYspeed(bullet_direction,speed));
                mainparent.addBullet(bullet_to_fire);
                fireCount = 0;
                return true;
            }
        }
        if(head_pos.y < mainparent.getHeight()/2 && x < player.getXpos() && direction == 0)
        {
            bulletX = x + 100;
            fireCount++;
            if(fireCount == FIRING_RATE)
            {
                new AePlayWave("Sounds\\sndGun.wav").start();
                Bullet bullet_to_fire = new Bullet(mainparent,bulletX, bulletY, Color.black, bullet_size,ATTACK_POWER);
                double bullet_direction = Worm.pointDirection(x, y, head_pos.x, head_pos.y);
                double speed = ATTACK_BULLET_SPEED;
                bullet_to_fire.setXYspeed(Worm.getXspeed(bullet_direction,speed), Worm.getYspeed(bullet_direction,speed));
                mainparent.addBullet(bullet_to_fire);
                fireCount = 0;
                return true;
            }
        }
        return false;
    }
}
